package com.xiguaji.tiktok.v1.tiktok.sys.controller;

import com.xiguaji.boot.toolkit.HttpUtil;
import com.xiguaji.tiktok.config.exception.ParamsException;
import com.xiguaji.tiktok.utils.CheckMobile;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.BaseController;
import com.xiguaji.tiktok.v1.tiktok.sys.model.SysUserVisitRecord;
import com.xiguaji.tiktok.v1.tiktok.sys.service.SysUserVisitRecordService;
import com.xiguaji.tiktok.v1.tiktok.sys.vo.VisitVo;
import com.xiguaji.tiktok.v1.user.model.User;
import io.swagger.annotations.ApiOperation;
import nl.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class SysController extends BaseController {
    @Autowired
    SysUserVisitRecordService sysUserVisitRecordService;

    @ApiOperation(value = "用户访问记录",notes = "用户访问记录")
    @RequestMapping(value="sys/visit/record", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String callbackOrder(@RequestBody VisitVo visitVo) throws ParamsException {
        // visitVo = getParam(VisitVo.class,body);
        SysUserVisitRecord sysUserVisitRecord = new SysUserVisitRecord();
        sysUserVisitRecord.setRequestUrl(HttpUtil.getRequest().getRequestURL().toString());
        sysUserVisitRecord.setMenuId(visitVo.getId());
        sysUserVisitRecord.setCreateTime(System.currentTimeMillis()/1000);
        sysUserVisitRecord.setDateCode(Integer.parseInt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))));
        sysUserVisitRecord.setRequestIp(HttpUtil.getIp());
        sysUserVisitRecord.setMobile(CheckMobile.check());
        sysUserVisitRecord.setRequestMethod(HttpUtil.getRequest().getMethod());
        //转成UserAgent对象
        UserAgent userAgent = UserAgent.parseUserAgentString(HttpUtil.getRequest().getHeader("User-Agent"));
        sysUserVisitRecord.setBrowser(userAgent.getBrowser().getName());
        sysUserVisitRecord.setOs(userAgent.getOperatingSystem().getName());
        User user = getUser();

        if(null == user){
            sysUserVisitRecord.setUserId(0l);
            sysUserVisitRecord.setVersionId(0l);
            sysUserVisitRecord.setUserKey(sysUserVisitRecord.getRequestIp());
        }else {
            sysUserVisitRecord.setUserId(user.getId());
            sysUserVisitRecord.setVersionId(getRoleId());
            sysUserVisitRecord.setUserKey(String.valueOf(user.getId()));
        }

        sysUserVisitRecord.setRequestCode("200");
        sysUserVisitRecord.setRequestMsg("SUCCESS");
        sysUserVisitRecordService.addOrUpdate(sysUserVisitRecord);
        return Ret.success("SUCCESS");
    }
}
